from flask import (Blueprint, request, session)
bp = Blueprint('changeSheetName', __name__, url_prefix='/changeSheetName')

from flaskr.server.SheetsServer import *

@bp.route('', methods = ['POST'])
def changeSheetName():
    old_sheet_name = request.json.get('oldSheetName')
    new_sheet_name = request.json.get('newSheetName')
    has_change = False
    sheets = []

    # if('sheets' in session.keys()):
    #     sheets = session['sheets']
    #
    #
    # for sheet in sheets:
    #     if(sheet['showName'] == old_sheet_name):
    #         sheet['showName'] = new_sheet_name
    #         has_change = True
    #
    #         session['sheets'] = sheets
    #         break

    sheet = get_sheet(old_sheet_name)

    if sheet:
        sheet['showName'] = new_sheet_name
        has_change = True

        set_sheet(old_sheet_name, sheet)
    
    if has_change:
        return {
            'code': 200,
            'msg': '修改成功',
            'data': sheets
        }

    else:
        return{
            'code': 500,
            'msg': '修改失败'
        }
